{layout name="layout/header" /}
<link href="root/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="root/js/plugins/fancybox/jquery.fancybox.css" rel="stylesheet">

<link href="root/css/plugins/switchery/switchery.css" rel="stylesheet">
<script src="root/js/plugins/switchery/switchery.js"></script>
<div class="wrapper wrapper-content animated fadeInRight" id="app">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>{$title}</h5>
                </div>
                <div class="ibox-content">
                    <p>
                        <button type="button" class="btn btn-primary btn-outline refresh">刷新</button>
                        <a type="button" class="btn btn-primary btn-outline" href="{:url('index')}">返回列表页</a>
                    </p>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal" id="form">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户名</label>
                            <div class="col-sm-10">
                                <input v-if="id==0" type="text" class="form-control" v-model="username"  name="username" placeholder="用户名">
                                <p v-else class="form-control-static">{{username}}</p>

                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户组</label>
                            <div class="col-sm-10">
                                <select class="form-control m-b" name="role_id" v-model="role_id">
                                    <option value="0" >请选择用户组</option>
                                    <option :value="i.id" v-for="i in role">{{i.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">登录密码</label>
                            <div class="col-sm-10">
                                <input type="password" name="password" v-model="password" id="password" class="form-control">
                                <span>登录密码不能少于6位</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">重复密码</label>
                            <div class="col-sm-10">
                                <input type="password" name="confirm_password" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">配置说明</label>
                            <div class="col-sm-10">
                                <p class="form-control-static">账号所能访问到的数据为配置的行政级别最低选中的一级，例如，配置四川省，其他都不选，那么用户可以访问所有四川省的数据，如果配置四川省成都市青羊区草市街道，那么可以访问草市街下所有社区的数据。</p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">行政区域</label>
                            <div class="col-sm-2">
                                <select class="form-control m-b" name="" v-model="province_id" @change="getArea(1,true)" :disabled="enable_province">
                                    <option value="0" >请选择省份</option>
                                    <option :value="i.id" v-for="i in province">{{i.name}}</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select class="form-control m-b" name="" v-model="city_id" @change="getArea(2,true)" :disabled="enable_city">
                                    <option value="0" >请选择市</option>
                                    <option :value="i.id" v-for="i in city">{{i.name}}</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select class="form-control m-b" name="" v-model="county_id" @change="getArea(3,true)" :disabled="enable_county">
                                    <option value="0" >请选择区/县</option>
                                    <option :value="i.id" v-for="i in county">{{i.name}}</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select class="form-control m-b" name="" v-model="town_id" @change="getArea(4,true)" :disabled="enable_town">
                                    <option value="0" >请选择乡/镇/街道</option>
                                    <option :value="i.id" v-for="i in town">{{i.name}}</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select class="form-control m-b" name="" v-model="village_id">
                                    <option value="0" >请选择社区/村</option>
                                    <option :value="i.id" v-for="i in village">{{i.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">备注</label>
                            <div class="col-sm-10">
                                <input type="text" name="remark" class="form-control" placeholder="备注" v-model="remark">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-10">
                                <div class="radio i-checks">
                                    <label><input type="radio" v-model="status" value="1" name="status"> <i></i>正常</label>
                                    <label><input type="radio" v-model="status" value="0" name="status"> <i></i>隐藏</label>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <a class="btn btn-primary" @click="submit" type="submit">保存内容</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- iCheck -->
<script src="root/js/plugins/iCheck/icheck.min.js"></script>
<script src="root/js/plugins/validate/jquery.validate.min.js"></script>
<script src="root/js/plugins/validate/messages_zh.min.js"></script>
<script src="root/js/plugins/fancybox/jquery.fancybox.js"></script>

<script>
    var data = {$data};

    var url = "{$url}";

    if (data == 1) {
        data = {
            id: 0,
            username:'',
            password:'',
            role_id:0,
            province_id:0,
            city_id:0,
            county_id:0,
            town_id:0,
            village_id:0,
            remark:'',
            status:1,
            level:0,
        }
    }
    data.enable_province = false;
    data.enable_city = false;
    data.enable_county = false;
    data.enable_town = false;
    data.role = {$role};
    data.admin = {$admin};
    data.province= [];
    data.city= [];
    data.county= [];
    data.town= [];
    data.village= [];
    //1
    new Vue({
        el: "#app",
        data: data,
        created:function(){
            var _this = this;
            _this.getArea(0);

            if(_this.admin.is_super){
                //超管编辑给与最高数据权限
                console.log(1)
                return;
            }
            //非超管 只能添加自己数据权限以下级别
            if(_this.admin.province_id != 0){
                _this.enable_province = true;
                _this.province_id = _this.admin.province_id
            }
            if(_this.admin.city_id != 0){
                _this.enable_city = true;
                _this.city_id = _this.admin.city_id
            }
            if(_this.admin.county_id != 0){
                _this.enable_county = true;
                _this.county_id = _this.admin.county_id
            }
            if(_this.admin.town_id != 0){
                _this.enable_town = true;
                _this.town_id = _this.admin.town_id
            }
            if(_this.province_id >0){
                _this.getArea(1,false);
            }
            if(_this.city_id >0){
                _this.getArea(2,false);
            }
            if(_this.county_id >0)
                _this.getArea(3,false);
            if(_this.town_id >0)
                _this.getArea(4,false);
            if(_this.village_id >0)
                _this.getArea(5,false);
        },
        methods: {
            getArea:function(level,init){
                var area_id;
                var _this= this;
                if(level == 0){
                    area_id = 0;
                }else if(level == 1){
                    area_id = _this.province_id;
                }else if(level ==2){
                    area_id = _this.city_id;
                }else if (level == 3){
                    area_id = _this.county_id;
                }else if(level==4){
                    area_id = _this.town_id;

                }
                if(init){
                    if(level < 2){
                        _this.city = [];
                        _this.city_id = 0;
                    }
                    if(level < 3){
                        _this.county = [];
                        _this.county_id = 0;
                    }
                    if(level < 4){
                        _this.town = [];
                        _this.town_id = 0;
                    }
                    if(level < 5){
                        _this.village = [];
                        _this.village_id = 0;
                    }
                }


                if(level != 0 && area_id ==0){
                    return ;
                }
                $.post("{:url('getArea')}",{pid:area_id},function (res) {
                    if(res.code){
                        if(level == 0){
                            _this.province = res.data;
                        }else if(level == 1){
                            _this.city = res.data;
                        }else if(level ==2){
                            _this.county = res.data;
                        }else if (level == 3){
                            _this.town = res.data;
                        }else if(level == 4){
                            _this.village = res.data
                        }
                    }
                },'json')
            },
            submit:function () {
                var _this = this;
                $.validator.addMethod("unique",function(value,element,params){
                    var a = 0;
                    $.ajax({
                        url:"{:url('ajax_unique_username')}",
                        type:"post",
                        dataType:"json",
                        async:false,
                        data:{username:value},
                        success:function (res) {
                            if(res.code == 1){
                                a = 1;
                            }
                        }
                    });

                    if(a == 0){
                        return true;
                    }else {
                        return false;
                    }
                },"用户名已存在");
                var rules = {
                    rules: {
                        username:{unique:true,required:true},
                        password: {
                            required: true,
                            minlength: 6
                        },
                        confirm_password: {
                            required: true,
                            minlength: 6,
                            equalTo: "#password"
                        },
                        role_id:{
                            min:1,
                        }
                    },
                    messages:{
                        role_id:{
                            min:'请选择一个用户组',
                        }
                    }
                }
                if(_this.id != 0){
                    rules = {
                        rules: {
                            username:{unique:true,required:true},
                            password: {
                                minlength: 6
                            },
                            confirm_password: {
                                minlength: 6,
                                equalTo: "#password"
                            },
                            role_id:{
                                min:1,
                            }
                        },
                        messages:{
                            role_id:{
                                min:'请选择一个用户组',
                            }
                        }
                    }
                }

                $("#form").validate(rules)
                if($("#form").valid()) {

                    $.post(url,data,function (res) {
                        layer.msg(res.msg);

                        if(res.code==1){
                            setTimeout(function () {
                                window.location.reload()
                            },1500)
                        }
                    },'json')
                }

            }
        },
        mounted: function () {
            checks();
            $('.fancybox').fancybox({
                openEffect: 'none',
                closeEffect: 'none'
            });
        }
    });
</script>

<script>
    $('input[name="status"]').on('ifChecked', function (event) {
        data.status = $(this).val()
    });

</script>
